<%--
  profile.jsp
  作用：用户个人中心页面，提供修改个人信息、密码和管理收货地址的功能。
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户中心</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/layout.css">
</head>
<body>

<div class="main-container">
    <jsp:include page="/WEB-INF/parts/sidebar.jsp" />

    <main class="content-area">
        <jsp:include page="/WEB-INF/parts/header.jsp" />

        <div class="page-title">
            <h2>用户中心</h2>
        </div>

        <%-- 显示操作结果消息 --%>
        <c:if test="${not empty message}">
            <p class="success-message">${message}</p>
        </c:if>

        <div class="profile-section">
            <h3>个人信息</h3>
            <form action="${pageContext.request.contextPath}/user/updateProfile" method="post" enctype="multipart/form-data">
                <div class="form-group avatar-group">
                    <label>我的头像</label>
                    <img src="${pageContext.request.contextPath}/uploads/avatars/${sessionScope.user.avatar}" alt="用户头像" class="current-avatar">
                    <input type="file" name="avatar" class="form-control-file">
                </div>
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" value="${sessionScope.user.username}" class="form-control">
                </div>
                 <div class="form-group">
                    <label for="phone">手机号</label>
                    <input type="text" id="phone" name="phone" value="${sessionScope.user.phone}" class="form-control">
                </div>
                <button type="submit" class="btn">保存信息</button>
            </form>
        </div>

        <div class="profile-section">
            <h3>修改密码</h3>
            <form action="${pageContext.request.contextPath}/user/updatePassword" method="post">
                <div class="form-group">
                    <label for="newPassword">新密码</label>
                    <input type="password" id="newPassword" name="newPassword" class="form-control" required>
                </div>
                <div class="form-group">
                    <label for="confirmPassword">确认新密码</label>
                    <input type="password" id="confirmPassword" name="confirmPassword" class="form-control" required>
                </div>
                <button type="submit" class="btn">更新密码</button>
            </form>
        </div>

        <div class="profile-section">
            <h3>收货地址管理</h3>
            <div class="address-list">
                <c:forEach var="addr" items="${addresses}">
                    <div class="address-item">
                        <p>${addr.address}</p>
                        <a href="${pageContext.request.contextPath}/user/deleteAddress?id=${addr.id}" class="btn-danger" onclick="return confirm('确定要删除这个地址吗？')">删除</a>
                    </div>
                </c:forEach>
                <c:if test="${empty addresses}">
                    <p>您还没有添加任何收货地址。</p>
                </c:if>
            </div>
            <form action="${pageContext.request.contextPath}/user/addAddress" method="post" class="add-address-form">
                 <div class="form-group">
                    <label for="newAddress">添加新地址</label>
                    <input type="text" id="newAddress" name="newAddress" class="form-control" required>
                </div>
                <button type="submit" class="btn">添加地址</button>
            </form>
        </div>

        <div class="profile-section">
            <h3>我的评价</h3>
            <div class="rating-summary">
                <p>平均评分: 
                    <c:choose>
                        <c:when test="${averageRating > 0}">
                            <strong>${String.format("%.1f", averageRating)}</strong> / 5.0
                        </c:when>
                        <c:otherwise>
                            暂无评分
                        </c:otherwise>
                    </c:choose>
                </p>
            </div>

            <div class="received-ratings-list">
                <h4>收到的所有评价</h4>
                <c:choose>
                    <c:when test="${not empty receivedRatings}">
                        <c:forEach var="rating" items="${receivedRatings}">
                            <div class="rating-item">
                                <div class="rating-header">
                                    <span>评价者: <a href="${pageContext.request.contextPath}/user/profile?id=${rating.rater.id}">${rating.rater.username}</a></span>
                                    <span>评分: <strong>${rating.score}</strong> / 5</span>
                                    <span>时间: ${rating.createdAt}</span>
                                </div>
                                <div class="rating-comment">
                                    <p>${not empty rating.comment ? rating.comment : "(无评论)"}</p>
                                </div>
                            </div>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <p>您目前还没有收到任何评价。</p>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>

    </main>
</div>
<script>
    // 简单的密码确认逻辑
    const passwordForm = document.querySelector('form[action$="updatePassword"]');
    passwordForm.addEventListener('submit', function(e) {
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        if (newPassword !== confirmPassword) {
            e.preventDefault();
            alert('两次输入的新密码不一致！');
        }
    });
</script>
</body>
</html> 